<% helpers.setHtmlTitle("Send Team Email"); %>
<% helpers.includeCss("pro/team_email_form.css"); %>

<% helpers.includeJQuery() %>
<% helpers.includeJs("pro/team_email_client.js"); %>

<% function allIds(editors) {
    var ids = [];
    editors.forEach(function(e) {
        ids.push(e.id);
    });
    return ids.join(',');
} %>

<div class="fpcontent">

    <h1>Send Email to Team Members</h1>
    
    <% if (! editors.length) { %>
    
        <div class="notice">
            <p class="statusMessage">Your team currently has no members.</p>
        </div>
        
    <% } else { %>
    
        <% if (result) { %>
            <div id="result" class="<%= result.status %>">
            
                <p class="statusMessage"><%= result.statusMessage %></p>
            
                <% if (result.sentTo) { %>
                    <p><strong>Message sent to:</strong> <%= result.sentTo %></p>
                <% } %>
                <% if (result.sentFailed) { %>
                    <p><strong>Message not sent to:</strong> <%= result.notSentTo %></p>
                <% } %>
                
                <% if (result.sentToFacilitator) { %>
                    <p>A copy of this message has been sent to the team facilitator.</p>
                <% } else { %>
                    <p>Due to server error, a copy of this message was not sent to the team facilitator.</p>
                <% } %>
                
                <% if (result.sentTo) { %>
                    <p><strong>Sent by:</strong> <%= result.fromAddr %></p>
                    <p><strong>Subject:</strong> <%= result.subject %></p>
                    <p><strong>Message:</strong><p>
                    <p><pre><%= result.message %></pre></p>
                <% } %>
            </div>   
        <% } %>
        
        <div id="form-error">Please correct the errors shown below and resubmit the form.</div>
        
        <form id="send-email" method="post" action="<%= request.path %>">
        
            <div class="send-email-form">
            
                <div class="forminner">
                
                    <div class="formfield input">
                        <label for="subject">Subject: <span class="field-error" id="errorMessage_subject">required</span></label>
                        <input class="required" type="text" name="subject" id="subject" value="" />
                    </div>
                    
                    <fieldset class="checkboxes">
                        <legend>Select Recipients: <span class="field-error" id="errorMessage_recipients">required</span></legend>
                            
                        <% if (editors.length > 1) { %>
                            <div class="formfield">
                                <input type="checkbox" name="recipients" id="recipients_all" value="<%= allIds(editors) %>" /><label for="recipients_all">All</label>
                            </div>
                        <% } %>
                        
                        <% var counter = 0;
                           editors.forEach(function(e) { 
                             var id = 'recipient_' + counter;
                        %>   
                             <div class="formfield">                                        
                                <input type="checkbox" name="recipients" id="<%= id %>" class="recipient_individual" value="<%= e.id %>" /><label for="<%= id %>"><%= e.fullName %></label> 
                             </div>
                             
                        <% counter++;                       
                        }); %>
                        
                    </fieldset>
                    
                    <div class="formfield textarea">
                        <label id="message-label" for="message">Message: <span class="field-error" id="errorMessage_message">required</span></label>
                        <input id="insert-pad-link" type="button" value="Insert link to pad" title="Use this button to insert a link to the pad in your email message" />
                        <textarea class="required" name="message" id="message"></textarea>
                    </div>
                
                </div>
            </div>
            
            <div class="buttons-wrap">
              <input class="submit" type="submit" name="submit" value="Send Email" />
              <input class="submit" type="button" name="cancel" value="Cancel" />
            </div>
        </form>        
    <% } %>
</div>
